package jerry.file.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashSet;

import jerry.sql.demo.DAO;

public class ImportDatas {

	private PreparedStatement getPlaceStatement() throws SQLException{
		Connection connection = DAO.getConnection();
		connection.setAutoCommit(false);
		PreparedStatement prepareStatement = connection.prepareStatement("INSERT INTO PLACE(NAME)  VALUES(?)");
		return prepareStatement;
	}
	
	private PreparedStatement getKeyWordsStatement() throws SQLException{
		Connection connection = DAO.getConnection();
		connection.setAutoCommit(false);
		PreparedStatement prepareStatement = connection.prepareStatement("INSERT INTO keywords(NAME) VALUES(?)");
		return prepareStatement;
	}
	
	public void importPlace() throws IOException, SQLException{
		BufferedReader bufferedReader = FileUtil.getBufferedReader("E:\\data\\place.txt", "utf-8");
		PreparedStatement placeStatement = getPlaceStatement();
		
		HashSet<String> hashSet = new HashSet<String>(5000);
		
		String currentString;
		long count = 0;
		while (true) {
			currentString = bufferedReader.readLine();
			if (currentString == null) break;
			if(hashSet.contains(currentString))	continue;
			placeStatement.setString(1, currentString);
			placeStatement.addBatch();
			
			count++;
			if(count % 10000 == 0){
				placeStatement.executeBatch();
				placeStatement.clearBatch();
				placeStatement.getConnection().commit();
				System.out.println("读取了："+count+"行");
			}
			if(hashSet.size() == 5000){
				hashSet.clear();
				System.out.println("插取了："+5000+"行");
				
			}
		}
		
		placeStatement.executeBatch();
		placeStatement.clearBatch();
		placeStatement.getConnection().commit();
		placeStatement.getConnection().close();
		bufferedReader.close();
	}
	
	public void importKeyWords() throws IOException{
		BufferedReader bufferedReader = FileUtil.getBufferedReader("E:\\data\\不重复\\keywords_clean.txt", "utf-8");
		PreparedStatement placeStatement;
		StringBuilder sb = null;
		long count = 0;
		try {
			placeStatement = getKeyWordsStatement();
			String currentString;
			sb = new StringBuilder(500000);  
			sb.append("INSERT INTO keywords(NAME) values");
			currentString = bufferedReader.readLine();
			sb.append("('").append(currentString).append("')");
			while (true) {
				currentString = bufferedReader.readLine();
				if (currentString == null) break;
				count++;
				sb.append(",");
				sb.append("('").append(currentString).append("')");
				
				if(count % 10000 == 0){
					placeStatement.executeUpdate(sb.toString());
					placeStatement.getConnection().commit();
					sb = new StringBuilder(500000);
					sb.append("INSERT INTO keywords(NAME) values");
					currentString = bufferedReader.readLine();
					if(currentString == null)	break;
					sb.append("('").append(currentString).append("')");
					System.out.println("读取了："+count+"行");
				}
				
			}
			placeStatement.executeUpdate(sb.toString());
			placeStatement.getConnection().commit();
			placeStatement.getConnection().close();
			bufferedReader.close();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(count);
			System.out.println(sb.toString());
		}
		
	}
	
	public static void main(String[] args) throws IOException, SQLException{
		ImportDatas importDatas = new ImportDatas();
		importDatas.importKeyWords();
	}
}
